﻿
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using static MongoDB.Bson.Serialization.BsonDeserializationContext;
using static MongoDB.Bson.Serialization.BsonSerializationContext;

namespace Common
{
    public class MongoDBHelper
    {

        private MongoClient client;
        private GridFSBucket bucket;
        private IMongoDatabase _db;
        private string dbName;
        private string ip;

        #region 构造函数

        public MongoDBHelper(string IP, int port, string dbName, string userName = "", string pwd = "")
        {
            try
            {
                SetMongo(IP, port, dbName, userName, pwd);
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        public MongoDBHelper(MongoClientSettings set, string dbName)
        {
            try
            {
                client = new MongoClient(set);
                this.dbName = dbName;
                _db = client.GetDatabase(dbName);
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType(), ex);
            }
        }

        #endregion

        #region setMongo

        public void SetMongo(string IP, int port, string dbName, string userName = "", string pwd = "")
        {
            ip = IP;
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(pwd))
            {
                client = new MongoClient("mongodb://" + IP + ":" + port);
            }
            else
            {
                client = new MongoClient(string.Format("mongodb://{0}:{1}@{2}:{3}", userName, pwd, IP, port));
            }

            this.dbName = dbName;
            _db = client.GetDatabase(dbName);
            bucket = new GridFSBucket(_db);
        }

        #endregion

        #region 获取表相关得方法

        public IList<BsonDocument> GetDocumentByCollectionName(string cName)
        {
            return _db.GetCollection<BsonDocument>(cName).Find(new BsonDocument()).ToList();
        }

        public List<BsonDocument> GetCollections()
        {
            return _db.ListCollections().ToList();
        }

        #endregion

        #region 连接状态

        public bool ConnectionStatus
        {
            get
            {
                try
                {
                    return IPHelper.PingIP(ip);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(this.GetType(), ex);
                    return false;
                }
            }
        }

        #endregion

        #region 文件操作相关

        /// <summary>
        /// 查询所有文件
        /// </summary>
        /// <returns></returns>
        public IList<GridFSFileInfo> FindAll()
        {
            return bucket.Find(FilterDefinition<GridFSFileInfo>.Empty).ToList();
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fullName"></param>
        public ObjectId UploadFile(string fullName)
        {
            if (string.IsNullOrEmpty(fullName)) return ObjectId.Empty;
            FileInfo fi = new FileInfo(fullName);
            return bucket.UploadFromStream(fi.Name, fi.OpenRead());
        }


        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fullName">包含路径得文件名</param>
        public void DownloadFile(ObjectId id, string fullName)
        {
            if (string.IsNullOrEmpty(fullName)) return;
            FileInfo f = new FileInfo(fullName);
            string dir = fullName.Replace(f.Name, "");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            FileStream fileStream = new FileStream(fullName, FileMode.OpenOrCreate);
            bucket.DownloadToStream(id, fileStream);
            fileStream.Dispose();
            fileStream.Close();
        }
        /// <summary>
        /// 删除指定得文件
        /// </summary>
        /// <param name="fileName"></param>
        public void DeleteFile(ObjectId id)
        {
            bucket.Delete(id);
        }

        #endregion

        #region 查询方法


        public T Find<T>(string id, string collectionName)
        {
            return _db.GetCollection<T>(collectionName).Find<T>(new BsonDocument("Name", "zhangsan")).FirstOrDefault();
        }


        /// <summary>
        ///     获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="id">objectId</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public T Get<T>(string id, string collectionName = null)
        {
            return GetAsync<T>(id, collectionName).Result;
        }

        public T Max<T>(string sortKey, string collectionName = null)
        {
            return _db.GetCollection<T>(collectionName).Find(FilterDefinition<T>.Empty).Sort(new BsonDocument(sortKey, "1")).FirstOrDefault();
        }

        public T Min<T>(string sortKey, string collectionName = null)
        {
            return _db.GetCollection<T>(collectionName).Find(FilterDefinition<T>.Empty).SortBy(O => sortKey).FirstOrDefault();
        }

        /// <summary>
        ///     获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public T Get<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            return GetAsync(expression, collectionName).Result;
        }

        /// <summary>
        ///     获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public T Get<T>(FilterDefinition<T> filter, string collectionName = null)
        {
            return GetAsync(filter, collectionName).Result;
        }

        /// <summary>
        ///     获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public TNewProjection Get<T, TNewProjection>(Expression<Func<T, bool>> expression,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            return GetAsync(expression, projection, collectionName).Result;
        }

        /// <summary>
        ///     获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">表名</param>
        /// <returns></returns>
        public TNewProjection Get<T, TNewProjection>(FilterDefinition<T> filter,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            return GetAsync(filter, projection, collectionName).Result;
        }

        /// <summary>
        ///     异步获取单个对象
        /// </summary>
        /// <param name="id">objectId</param>
        /// <param name="collectionName">表名</param>
        /// <returns></returns>
        public async Task<T> GetAsync<T>(string id, string collectionName)
        {
            return await GetAsync<T>(new BsonDocument("_id", id), collectionName).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取单个对象
        /// </summary>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">表名</param>
        /// <returns></returns>
        public async Task<T> GetAsync<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(expression).FirstOrDefaultAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">表名称</param>
        /// <returns></returns>
        public async Task<TNewProjection> GetAsync<T, TNewProjection>(Expression<Func<T, bool>> expression,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(expression).Project(projection).FirstOrDefaultAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取单个对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">表名</param>
        /// <returns></returns>
        public async Task<TNewProjection> GetAsync<T, TNewProjection>(FilterDefinition<T> filter,
            Expression<Func<T, TNewProjection>> projection, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(filter).Project(projection).FirstOrDefaultAsync().ConfigureAwait(false);
        }

        public async Task<T> GetAsync<T>(FilterDefinition<T> filter, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(filter).FirstOrDefaultAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     获取集合
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll<T>(string collectionName = null)
        {
            return GetAllAsync<T>(collectionName).Result;
        }

        /// <summary>
        ///     异步获取集合
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync<T>(string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(new BsonDocument()).ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<T> Find<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            return FindAsync(expression, collectionName).Result;
        }

        /// <summary>
        ///     查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<TNewProjection> Find<T, TNewProjection>(Expression<Func<T, bool>> expression,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            return FindAsync(expression, projection, collectionName).Result;
        }

        /// <summary>
        ///     查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<T> Find<T>(FilterDefinition<T> filter, string collectionName = null)
        {
            return FindAsync(filter, collectionName).Result;
        }

        /// <summary>
        ///     查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<TNewProjection> Find<T, TNewProjection>(FilterDefinition<T> filter,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            return FindAsync(filter, projection, collectionName).Result;
        }

        /// <summary>
        ///     获取记录数
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public long Count<T>(string collectionName = null)
        {
            return CountAsync<T>(new BsonDocument(), collectionName).Result;
        }

        /// <summary>
        ///     获取记录数
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public long Count<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            return CountAsync(expression, collectionName).Result;
        }

        /// <summary>
        ///     获取记录数
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public long Count<T>(FilterDefinition<T> filter, string collectionName = null)
        {
            return CountAsync(filter, collectionName).Result;
        }

        /// <summary>
        ///     异步获取记录数
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<long> CountAsync<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.CountAsync(expression).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取记录数
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<long> CountAsync<T>(FilterDefinition<T> filter, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.CountAsync(filter).ConfigureAwait(false);
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<T> PageList<T>(int pageIndex, int pageSize, string collectionName = null)
        {
            return PageListAsync<T>(pageIndex, pageSize, collectionName).Result;
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<TNewProjection> PageList<T, TNewProjection>(int pageIndex, int pageSize,
            Expression<Func<T, TNewProjection>> projection, string collectionName)
        {
            return PageListAsync(pageIndex, pageSize, projection, collectionName).Result;
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="where">筛选条件</param>
        /// <param name="sort">排序条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<T> PageList<T>(int pageIndex, int pageSize, Expression<Func<T, bool>> where,
            SortDefinition<T> sort, string collectionName = null)
        {
            return PageListAsync(pageIndex, pageSize, where, sort, collectionName).Result;
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="where">筛选条件</param>
        /// <param name="sort">排序条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<TNewProjection> PageList<T, TNewProjection>(int pageIndex, int pageSize,
            Expression<Func<T, bool>> where, SortDefinition<T> sort, Expression<Func<T, TNewProjection>> projection,
            string collectionName = null)
        {
            return PageListAsync(pageIndex, pageSize, where, sort, projection, collectionName).Result;
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sort">排序条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<T> PageList<T>(int pageIndex, int pageSize, FilterDefinition<T> filter,
            SortDefinition<T> sort, string collectionName)
        {
            return PageListAsync(pageIndex, pageSize, filter, sort, collectionName).Result;
        }

        /// <summary>
        ///     获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="filter">过滤器</param>
        /// <param name="sort">排序条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public List<TNewProjection> PageList<T, TNewProjection>(int pageIndex, int pageSize,
            FilterDefinition<T> filter,
            SortDefinition<T> sort, Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            return PageListAsync(pageIndex, pageSize, filter, sort, projection, collectionName).Result;
        }

        /// <summary>
        ///     异步获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<T>> PageListAsync<T>(int pageIndex, int pageSize, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return
                await
                    collection.Find(new BsonDocument())
                        .Skip((pageIndex - 1) * pageSize)
                        .Limit(pageSize)
                        .ToListAsync()
                        .ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<TNewProjection>> PageListAsync<T, TNewProjection>(int pageIndex, int pageSize,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return
                await
                    collection.Find(new BsonDocument())
                        .Project(projection)
                        .Skip((pageIndex - 1) * pageSize)
                        .Limit(pageSize)
                        .ToListAsync()
                        .ConfigureAwait(false);
        }

        public async Task<List<T>> PageListAsync<T>(int pageIndex, int pageSize, FilterDefinition<T> filter,
            SortDefinition<T> sort, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return
                await
                    collection.Find(filter)
                        .Sort(sort)
                        .Skip((pageIndex - 1) * pageSize)
                        .Limit(pageSize)
                        .ToListAsync()
                        .ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="filter">过滤器</param>
        /// <param name="sort">排序条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<TNewProjection>> PageListAsync<T, TNewProjection>(int pageIndex, int pageSize,
            FilterDefinition<T> filter, SortDefinition<T> sort, Expression<Func<T, TNewProjection>> projection,
            string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return
                await
                    collection.Find(filter)
                        .Project(projection)
                        .Sort(sort)
                        .Skip((pageIndex - 1) * pageSize)
                        .Limit(pageSize)
                        .ToListAsync()
                        .ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="where">筛选条件</param>
        /// <param name="sort">排序条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<T>> PageListAsync<T>(int pageIndex, int pageSize, Expression<Func<T, bool>> where,
            SortDefinition<T> sort, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return
                await
                    collection.Find(where)
                        .Sort(sort)
                        .Skip((pageIndex - 1) * pageSize)
                        .Limit(pageSize)
                        .ToListAsync()
                        .ConfigureAwait(false);
        }

        /// <summary>
        ///     异步获取分页数据
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="pageIndex">页次</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="where">筛选条件</param>
        /// <param name="sort">排序条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<TNewProjection>> PageListAsync<T, TNewProjection>(int pageIndex, int pageSize,
            Expression<Func<T, bool>> where, SortDefinition<T> sort, Expression<Func<T, TNewProjection>> projection,
            string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return
                await
                    collection.Find(where)
                        .Project(projection)
                        .Sort(sort)
                        .Skip((pageIndex - 1) * pageSize)
                        .Limit(pageSize)
                        .ToListAsync()
                        .ConfigureAwait(false);
        }

        /// <summary>
        ///     异步查询
        /// </summary>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<T>> FindAsync<T>(Expression<Func<T, bool>> expression,
            string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(expression).ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     异步查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="expression">查询条件</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<TNewProjection>> FindAsync<T, TNewProjection>(
            Expression<Func<T, bool>> expression,
            Expression<Func<T, TNewProjection>> projection, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(expression).Project(projection).ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     异步查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<T>> FindAsync<T>(FilterDefinition<T> filter, string collectionName)
        {

            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(filter).Project<T>(Builders<T>.Projection.Exclude("_class").Exclude("_t")).ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        ///     异步查询
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <typeparam name="TNewProjection">新实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="projection">新实体映射</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<List<TNewProjection>> FindAsync<T, TNewProjection>(FilterDefinition<T> filter,
            Expression<Func<T, TNewProjection>> projection, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.Find(filter).Project(projection).ToListAsync().ConfigureAwait(false);
        }

        #endregion

        #region 插入方法

        /// <summary>
        ///     插入
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="value">对象</param>
        /// <param name="collectionName">集合名称</param>
        public void Insert<T>(T value, string collectionName = null)
        {
            InsertAsync(value, collectionName).Wait();
        }

        /// <summary>
        ///     异步插入
        /// </summary>
        /// <param name="value">对象</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task InsertAsync<T>(T value, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            await collection.InsertOneAsync(value).ConfigureAwait(false);
        }

        /// <summary>
        ///     批量插入
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="values">对象集合</param>
        /// <param name="collectionName">集合名称</param>
        public void BatchInsert<T>(IEnumerable<T> values, string collectionName = null)
        {
            BatchInsertAsync(values, collectionName).Wait();
        }

        /// <summary>
        ///     异步批量插入
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="values">对象集合</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task BatchInsertAsync<T>(IEnumerable<T> values, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            await collection.InsertManyAsync(values).ConfigureAwait(false);
        }

        #endregion

        #region 更新方法

        /// <summary>
        ///     覆盖更新
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="value">对象</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public ReplaceOneResult Update<T>(T value, string collectionName = null)
        {
            return UpdateAsync(value, collectionName).Result;
        }

        /// <summary>
        ///     局部更新
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="id">记录ID</param>
        /// <param name="update">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public UpdateResult Update<T>(ObjectId id, UpdateDefinition<T> update, string collectionName = null)
        {
            return UpdateAsync(new BsonDocument("_id", id), update, collectionName).Result;
        }

        /// <summary>
        ///     局部更新
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="update">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public UpdateResult Update<T>(Expression<Func<T, bool>> expression, UpdateDefinition<T> update,
            string collectionName = null)
        {
            return UpdateAsync(expression, update, collectionName).Result;
        }
        /// <summary>
        ///     局部更新
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="update">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public UpdateResult UpdateMany<T>(Expression<Func<T, bool>> expression, UpdateDefinition<T> update,
            string collectionName = null)
        {
            return UpdateManyAsync(expression, update, collectionName).Result;
        }

        public UpdateResult Update<T>(FilterDefinition<T> expression, UpdateDefinition<T> update,
            string collectionName = null)
        {
            return UpdateAsync(expression, update, collectionName).Result;
        }

        /// <summary>
        ///     异步局部更新（仅更新一条记录）
        ///     <para><![CDATA[expression 参数示例：x => x.Id == 1 && x.Age > 18 && x.Gender == 0]]></para>
        ///     <para><![CDATA[entity 参数示例：y => new T{ RealName = "Ray", Gender = 1}]]></para>
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="entity">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public UpdateResult Update<T>(Expression<Func<T, bool>> expression, Expression<Action<T>> entity,
            string collectionName = null)
        {
            return UpdateAsync(expression, entity, collectionName).Result;
        }

        /// <summary>
        ///     异步局部更新（仅更新一条记录）
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="update">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<UpdateResult> UpdateAsync<T>(FilterDefinition<T> filter, UpdateDefinition<T> update,
            string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.UpdateOneAsync(filter, update).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步局部更新（仅更新一条记录）
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="update">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<UpdateResult> UpdateAsync<T>(Expression<Func<T, bool>> expression,
            UpdateDefinition<T> update, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.UpdateOneAsync(expression, update).ConfigureAwait(false);
        }
        /// <summary>
        /// 异步局部更新（仅更新多条记录）
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="update">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<UpdateResult> UpdateManyAsync<T>(Expression<Func<T, bool>> expression,
            UpdateDefinition<T> update, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.UpdateManyAsync(expression, update).ConfigureAwait(false);
        }


        /// <summary>
        ///     异步局部更新（仅更新一条记录）
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="entity">更新条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<UpdateResult> UpdateAsync<T>(Expression<Func<T, bool>> expression,
            Expression<Action<T>> entity, string collectionName = null)
        {
            var fieldList = new List<UpdateDefinition<T>>();

            var param = entity.Body as MemberInitExpression;
            if (param != null)
            {
                foreach (var item in param.Bindings)
                {
                    var propertyName = item.Member.Name;
                    object propertyValue = null;
                    var memberAssignment = item as MemberAssignment;
                    if (memberAssignment == null) continue;
                    if (memberAssignment.Expression.NodeType == ExpressionType.Constant)
                    {
                        var constantExpression = memberAssignment.Expression as ConstantExpression;
                        if (constantExpression != null)
                            propertyValue = constantExpression.Value;
                    }
                    else
                    {
                        propertyValue = Expression.Lambda(memberAssignment.Expression, null).Compile().DynamicInvoke();
                    }

                    if (propertyName != "_id") //实体键_id不允许更新
                    {
                        fieldList.Add(Builders<T>.Update.Set(propertyName, propertyValue));
                    }
                }
            }
            var collection = _db.GetCollection<T>(collectionName);
            return
                await collection.UpdateOneAsync(expression, Builders<T>.Update.Combine(fieldList)).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步覆盖更新
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="value">对象</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<ReplaceOneResult> UpdateAsync<T>(T value, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await
                collection.ReplaceOneAsync(
                    new BsonDocument("_id", new ObjectId(typeof(T).GetProperty("Id").GetValue(value).ToString())),
                    value).ConfigureAwait(false);
        }
        /// <summary>
        ///     异步覆盖更新
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="value">对象</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<ReplaceOneResult> UpdateAsync<T>(T value, ObjectId Id, string collectionName)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await
                collection.ReplaceOneAsync(
                    new BsonDocument("_id", Id),
                    value).ConfigureAwait(false);
        }


        #endregion

        #region 删除方法

        /// <summary>
        ///     删除指定对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="id">对象Id</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public DeleteResult Delete<T>(ObjectId id, string collectionName = null)
        {
            return DeleteAsync<T>(id, collectionName).Result;
        }

        /// <summary>
        ///     删除指定对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">查询条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public DeleteResult Delete<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            return DeleteAsync(expression, collectionName).Result;
        }

        /// <summary>
        ///     异步删除指定对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="id">对象Id</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<DeleteResult> DeleteAsync<T>(ObjectId id, string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.DeleteOneAsync(new BsonDocument("_id", id)).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步删除指定对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">查询条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<DeleteResult> DeleteAsync<T>(Expression<Func<T, bool>> expression,
            string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.DeleteOneAsync(expression).ConfigureAwait(false);
        }

        /// <summary>
        ///     批量删除对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="ids">ID集合</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public DeleteResult BatchDelete<T>(IEnumerable<ObjectId> ids, string collectionName = null)
        {
            return BatchDeleteAsync<T>(ids, collectionName).Result;
        }

        /// <summary>
        ///     批量删除对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public DeleteResult BatchDelete<T>(FilterDefinition<T> filter, string collectionName = null)
        {
            return BatchDeleteAsync(filter, collectionName).Result;
        }

        /// <summary>
        ///     批量删除对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public DeleteResult BatchDelete<T>(Expression<Func<T, bool>> expression, string collectionName = null)
        {
            return BatchDeleteAsync(expression, collectionName).Result;
        }

        /// <summary>
        ///     异步批量删除对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="ids">ID集合</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<DeleteResult> BatchDeleteAsync<T>(IEnumerable<ObjectId> ids,
            string collectionName = null)
        {
            var filter = Builders<T>.Filter.In("_id", ids);
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.DeleteManyAsync(filter).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步批量删除对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="filter">过滤器</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<DeleteResult> BatchDeleteAsync<T>(FilterDefinition<T> filter,
            string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.DeleteManyAsync(filter).ConfigureAwait(false);
        }

        /// <summary>
        ///     异步批量删除对象
        /// </summary>
        /// <typeparam name="T">数据库实体类型</typeparam>
        /// <param name="expression">筛选条件</param>
        /// <param name="collectionName">集合名称</param>
        /// <returns></returns>
        public async Task<DeleteResult> BatchDeleteAsync<T>(Expression<Func<T, bool>> expression,
            string collectionName = null)
        {
            var collection = _db.GetCollection<T>(collectionName);
            return await collection.DeleteManyAsync(expression).ConfigureAwait(false);
        }

        #endregion
    }
}
